package handler

import (
	"net/http"

	jwt "github.com/appleboy/gin-jwt/v2"
	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"

	"github.com/super-team/start-kit/configs"
	"github.com/super-team/start-kit/handler/admin"
	"github.com/super-team/start-kit/handler/common"
	"github.com/super-team/start-kit/handler/menu"
	"github.com/super-team/start-kit/handler/response"
	"github.com/super-team/start-kit/handler/role"
	"github.com/super-team/start-kit/handler/roleMenu"
	jwtMiddleware "github.com/super-team/start-kit/middlewares/jwt"
	"github.com/super-team/start-kit/middlewares/permission"
)

//注册路由
func RegisterRoutes(router *gin.Engine) {

	router.GET("/health", func(ctx *gin.Context) {
		ctx.JSON(http.StatusOK, gin.H{"status": "ok"})
	})

	auth := jwtMiddleware.Auth()
	per := permission.AuthCheckRole()
	checkToken := auth.MiddlewareFunc()
	g := router.Group("/api")

	routes := []func(g *gin.RouterGroup, handlers ...gin.HandlerFunc){
		admin.Route,
		role.Route,
		menu.Route,
		common.Route,
		roleMenu.Route,
	}

	for _, route := range routes {
		route(g, checkToken, per)
	}

	registerPProf(router)
	registerJwt(g, auth)

	router.NoRoute(func(context *gin.Context) {
		context.JSON(http.StatusNotFound, response.Response{
			Code: http.StatusNotFound,
			Msg:  http.StatusText(http.StatusNotFound),
			Data: nil,
		})
		context.Abort()
	})

	router.NoMethod(func(context *gin.Context) {
		context.JSON(http.StatusMethodNotAllowed, response.Response{
			Code: http.StatusMethodNotAllowed,
			Msg:  http.StatusText(http.StatusMethodNotAllowed),
			Data: nil,
		})
		context.Abort()
	})

}

//pprof分析
func registerPProf(router *gin.Engine) {
	adminGroup := router.Group("/d", func(c *gin.Context) {
		if c.Query("token") != configs.Config.Server.PProfToken {
			c.AbortWithStatus(http.StatusForbidden)
			return
		}
		c.Next()
	})
	pprof.RouteRegister(adminGroup, "pprof")
}

//jwt相关
func registerJwt(r *gin.RouterGroup, auth *jwt.GinJWTMiddleware) {
	r.POST("/login", Login(auth))
	r.POST("/logout", Logout(auth))
	r.GET("/refresh", Refresh(auth))
}

// @Summary 登录
// @Description 登录
// @Tags 基础API
// @Accept json
// @Param data body jwtMiddleware.Login true "body"
// @Success 200 {string} string	"{"code": 200,"data": {"expire": 1602814892,"token": "Bearer eyJhbGk"},"msg": "登录成功"}"
// @Failure 401 {string} string	"{"code": 401, "data": null, "msg": "incorrect Username or Password" }"
// @Router /api/login [post]
func Login(auth *jwt.GinJWTMiddleware) func(ctx *gin.Context) {
	return auth.LoginHandler
}

// @Summary 退出登录
// @Description 退出登录
// @Tags 基础API
// @Accept json
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} string	"{"code": 200,"data": null,"msg": "退出登录成功"}"
// @Router /api/logout [post]
func Logout(auth *jwt.GinJWTMiddleware) func(ctx *gin.Context) {
	return auth.LogoutHandler
}

// @Summary 刷新token
// @Description 刷新token
// @Tags 基础API
// @Accept json
// @Param Authorization header string false "Bearer 用户令牌"
// @Success 200 {string} string	"{"code": 200,"data": {"expire": 1602814892,"token": "Bearer eyJhbGk"},"msg": "刷新token成功"}"
// @Failure 401 {string} string	"{"code": 401,"data": null,"msg": "token已过期"}"
// @Router /api/refresh [get]
func Refresh(auth *jwt.GinJWTMiddleware) func(ctx *gin.Context) {
	return auth.RefreshHandler
}
